﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using System.Threading;
using System.Net;

namespace AutoBuyWinform
{
    public partial class LogForm : Form
    {
        //StringBuilder logText;
        Product product;
        CookieContainer ccEntered;
        Thread t;
        int logLength = 1024;

        public LogForm()
        {
            InitializeComponent();
        }

        public LogForm(Product product, CookieContainer ccEntered)
        {
            InitializeComponent();
            //this.logText = new StringBuilder();
            this.product = product;
            this.ccEntered = ccEntered;
        }

        private void LogForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            this.Hide();
        }

        public void start()
        {
            t = new Thread(() =>
            {
                bool success = false;
                PostGoodsToCart pc = new PostGoodsToCart(product, ccEntered);
                success = pc.doPost(this);
                if (!success)
                {
                    this.Invoke(new Action(() =>
                    {
                        this.setMsg("加入购物车失败");
                    }));
                    //MessageBox.Show("加入购物车失败");
                    return;
                }

                //yj_hidden
                //加入购物车成功后，按需进行议价
                /*
                if (product.yj[0] != 0)
                {
                    YjPage yjPage = new YjPage();
                    success = pc.createYj(this, yjPage);
                    if (!success)
                    {
                        this.Invoke(new Action(() =>
                        {
                            this.setMsg("议价生成订单失败");
                        }));
                        //MessageBox.Show("议价生成订单失败");
                    }
                    else
                    {
                        this.Invoke(new Action(() =>
                        {
                            this.setMsg("议价生成订单成功！");
                        }));
                        //MessageBox.Show("议价生成订单成功！");
                    }
                    
                }
                */

                //加入购物车后，按需直接生成订单。如果议价失败也直接生成订单
                if (product.yj[0] == 0 || !success)
                {
                    success = pc.createOrder(this);
                    if (!success)
                    {
                        this.Invoke(new Action(() =>
                        {
                            this.setMsg("直接生成订单失败");
                        }));
                        //MessageBox.Show("直接生成订单失败");
                        return;
                    }

                    this.Invoke(new Action(() =>
                    {
                        this.setMsg("直接生成订单成功！");
                    }));
                    //MessageBox.Show("直接生成订单成功！");
                }

                

                Application.Run();
            });
            t.SetApartmentState(ApartmentState.STA);
            t.IsBackground = true;
            t.Start();
            //threadContainer.AddLast(t);
        }

        public void setMsg(String msg)
        {
            //if (logText.Length > logLength)
            //{
            //    logText = logText.Remove(logLength, logText.Length - logLength);
            //}
            //String dt = DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString();
            //logText.Append(dt + ":" + msg + "\r\n", 0, dt.Length+msg.Length + 3);
            logBox.Text = DateTime.Now + ":" + msg + "\r\n" + logBox.Text.Substring(0, logBox.Text.Length > logLength ? logLength : logBox.Text.Length);
            Refresh();
        }

    }
}
